package com.example.jedis.client.utils;

import com.example.jedis.client.protocol.constant.RedisProtocolConstant;
import redis.clients.jedis.exceptions.JedisException;

import java.nio.charset.Charset;

public class RedisProtocolUtils {

    public static  byte[] toBytes(String value){
        try {
            return value.getBytes(RedisProtocolConstant.CHARSET);
        }catch (Exception e){
            throw  new JedisException(e);
        }
    }
    public static  byte[] toBytes(int value){
      return toBytes(String.valueOf(value));
    }
    public  static String getStringFromBytes( byte [] data,int start,int end){

        String value =  new String(data,start,end, Charset.forName(RedisProtocolConstant.CHARSET)) ;
        return value;
    }

    public static int getIntFromBytes(byte [] data,int start,int end){
        String value = getStringFromBytes(data,start,end);
       return Integer.parseInt(value);
    }
}
